package com.atguigu.gmall.mq.receiver;

import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 延迟消息接受
 */
@Component
public class DeadLetterReceiver {

    /**
     * 基础延迟队列
     * @param message
     * @param mesag
     * @param channel
     * @throws IOException
     */
    @RabbitListener(queues = DeadLetterMqConfig.queue_dead_2)
    public void send(String message, Message mesag, Channel channel) throws IOException {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(format.format(new Date()));
        System.out.println("接受到的消息："+message);
        long deliveryTag = mesag.getMessageProperties().getDeliveryTag();

        channel.basicAck(deliveryTag,false);
    }


    /**
     * 基础插件发送消息
     * @param message
     * @param mesag
     * @param channel
     * @throws IOException
     */
    @RabbitListener(queues = DelayedMqConfig.queue_delay_1)
    public void send2(String message, Message mesag, Channel channel) throws IOException {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("接受时间"+format.format(new Date()));
        System.out.println("接受到的消息："+message);
        long deliveryTag = mesag.getMessageProperties().getDeliveryTag();

        channel.basicAck(deliveryTag,false);
    }
}
